package serv

import (
	"github.com/pkg/errors"
	"github.com/xuri/excelize/v2"
	"gorm.io/gorm"
	"server/internal/app/model"
	"server/internal/database"
	"time"
)

var EnterpriseUpdate = new(enterpriseUpdate)

type enterpriseUpdate struct {
	model.EnterpriseUpdate
	TissueName               string `json:"tissue_name"`
	EnterpriseUpdateTypeName string `json:"enterpriseUpdate_type_name"`
}

// Reads 列表
func (a enterpriseUpdate) Reads(Db *gorm.DB) (*gorm.DB, []enterpriseUpdate, int64) {
	var list []enterpriseUpdate
	var total int64
	Db.Model(&a).
		Select("enterprise_update.*,(select name from tissue where id = enterprise_update.tissue_id) as tissue_name," +
			"(select name from enterprise_type where id = enterprise_update.unit_type) as enterprise_type_name").
		Order("id desc").
		Find(&list).Offset(-1).Select("id").Count(&total)
	return Db, list, total
}

// Read 单条
func (a enterpriseUpdate) Read(id any) (*gorm.DB, enterpriseUpdate) {
	Db := database.DB
	detail := enterpriseUpdate{}
	Db.Model(&a).Where("id =?", id).First(&detail)

	return Db, detail

}

// Create 新增
func (a enterpriseUpdate) Create(data model.EnterpriseUpdate) *gorm.DB {
	Db := database.DB
	t := time.Now()
	data.CreateTime = int(t.Unix())
	data.UpdateTime = int(t.Unix())
	data.City = Tissue.GetAreaName(data.AreaId)
	data.Tissue = Tissue.GetAreaName(data.TissueId)
	return Db.Create(&data)
}

// Update 编辑
func (a enterpriseUpdate) Update(data model.EnterpriseUpdate) *gorm.DB {
	Db := database.DB
	t := time.Now()
	data.UpdateTime = int(t.Unix())
	data.City = Tissue.GetAreaName(data.AreaId)
	data.Tissue = Tissue.GetAreaName(data.TissueId)
	return Db.Save(&data)

}

// Delete 删除
func (a enterpriseUpdate) Delete(ids []int) *gorm.DB {
	Db := database.DB

	return Db.Delete(&a, ids)

}

// GenerateXlsx 导出Excel
func (enterpriseUpdate) GenerateXlsx(data [][]any, fileName string) (string, error) {
	f := excelize.NewFile()
	sheetName := "Sheet1"
	// 创建一个工作表
	err := f.SetSheetName("Sheet1", sheetName)

	if err != nil {
		return "", err
	}

	err = f.SetColWidth("Sheet1", "A", "B", 40)
	err = f.SetColWidth("Sheet1", "C", "C", 20)
	err = f.SetColWidth("Sheet1", "E", "E", 50)
	err = f.SetColWidth("Sheet1", "J", "K", 20)
	if err != nil {
		return "", err
	}
	for k, row := range data {
		startCell, err := excelize.JoinCellName("A", k+1)
		if err != nil {
			return "", err
		}
		err = f.SetSheetRow(sheetName, startCell, &row)
		if err != nil {
			return "", err
		}

	}

	t := time.Now()
	fileName = "public/excel/" + fileName + "-" + t.Format("20060102150405") + ".xlsx"
	// 根据指定路径保存文件
	err = f.SaveAs(fileName)
	if err != nil {
		return "", err

	}
	return fileName, nil
}

// Passing 审核通过
func (a enterpriseUpdate) Passing(id int) error {

	_, detail := a.Read(id)

	if detail.ID == 0 {
		return errors.New("编辑失败")
	}
	Db := database.DB
	Db.Model(&a.EnterpriseUpdate).Where("id=?", id).Update("status", 1)
	detail.Enterprise.ID = detail.EnterpriseId
	return Enterprise.Update(detail.Enterprise).Error

}

// Reject 审核驳回
func (a enterpriseUpdate) Reject(id int) error {

	Db := database.DB

	return Db.Model(&a.EnterpriseUpdate).Where("id=?", id).Update("status", 2).Error

}
